unit frmListFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, dmdIBDataModule, DB;

type
  TListFrame = class(TFrame)
    grpData: TGroupBox;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    grdList: TDBGrid;
    btnRaadplegen: TButton;
    btnToevoegen: TButton;
    btnWijzigen: TButton;
    btnVerwijderen: TButton;
    grpZoeken: TGroupBox;
    cmbVeld: TComboBox;
    rbtOplopend: TRadioButton;
    rbtAflopend: TRadioButton;
    txtZoek: TEdit;
    dtsGrid: TDataSource;

  private
    Tbls: Array of Array[0..1] of String;
    Flds: Array of Array[0..1] of String;
    Conditions: Array of String;
    Keyword: String;
    Descending: Boolean;

  public
    constructor Create(AOwner: TComponent); override;
    procedure updateSQL();
    procedure addTable(const name: String; const alias: String);
    procedure addField(const name: String; const alias: String);
    procedure addCondition(const cond: String);

  end;

implementation

uses frmMainForm;

{$R *.dfm}

{ TListFrame }


{ TListFrame }

procedure TListFrame.addCondition(const cond: String);
begin
  {Voeg Conditie's toe aan de Query bv WHERE, AND, ORDER BY, GROUP BY, etc...}
  if not (cond = '') then
  begin
    setLength(Conditions, (Length(Conditions)+1));
    Conditions[(Length(Conditions)-1)] := cond;
  end;
end;

procedure TListFrame.addField(const name: String; const alias: String);
begin
  {Voeg velden toe voor de Query die de data moet verzorgen}
  {Geef je een alias op dan moet je deze verder te gebruiken ipv de veldnaam}
  {Heb je een alias opgegeven bij de tabelnaam zet deze dan voor de veldnaam en daarachter een: . }
  {Heb je geen alias opgegevens bij de tablenaam en gebruik je wel meerdere tabellen zet de tabelnaam er dan in z'n geheel voor}
  if not (name = '') then
  begin
    SetLength(Flds, (Length(Flds)+1));
    Flds[(Length(Flds)-1)][0] := name;
    Flds[(Length(Flds)-1)][1] := alias;
  end;
end;

procedure TListFrame.addTable(const name: String; const alias: String);
begin
  {Voeg tabellen toe voor de Query die de data moet verzorgen}
  {Geef je een alias op dan moet je deze verder te gebruiken ipv de tabelnaam}
  if not (name = '') then
  begin
    setLength(Tbls, (Length(tbls)+1));
    Tbls[(Length(Tbls)-1)][0] := name;
    Tbls[(Length(Tbls)-1)][1] := alias;
  end;
end;


constructor TListFrame.Create(AOwner: TComponent);
begin
  inherited;

end;

procedure TListFrame.updateSQL;
var
  I: Integer;

begin
  IBDataModule.IBQuery.SQL.add('SELECT');
  for I := 0 to Length(Flds) - 1 do
  begin    
    if (Flds[I][1] = '')and not (I = (Length(Flds)-1)) then
      IBDataModule.IBQuery.SQL.add(Flds[I][0] + ',')
    else if not (Flds[I][1] = '')and not (I = (Length(Flds)-1)) then
      IBDataModule.IBQuery.SQL.add(Flds[I][0] + ' AS ' + Flds[I][1] + ',')
    else if (Flds[I][1] = '')and (I = (Length(Flds)-1)) then
      IBDataModule.IBQuery.SQL.add(Flds[I][0])
    else if not (Flds[I][1] = '')and (I = (Length(Flds)-1)) then
      IBDataModule.IBQuery.SQL.add(Flds[I][0] + ' AS ' + Flds[I][1]);
  end;
  IBDataModule.IBQuery.SQL.add('FROM');
  for I := 0 to Length(Tbls) - 1 do
  begin
    if (Tbls[I][1] = '')and not (I = (Length(Tbls)-1)) then
      IBDataModule.IBQuery.SQL.add(Tbls[I][0] + ',')
    else if not (Tbls[I][1] = '')and not (I = (Length(Tbls)-1)) then
      IBDataModule.IBQuery.SQL.add(Tbls[I][0] + ' ' + Tbls[I][1] + ',')
    else if (Tbls[I][1] = '')and (I = (Length(Tbls)-1)) then
      IBDataModule.IBQuery.SQL.add(Tbls[I][0])
    else if not (Tbls[I][1] = '')and (I = (Length(Tbls)-1)) then
      IBDataModule.IBQuery.SQL.add(Tbls[I][0] + ' ' + Tbls[I][1]);
  end;
  for I := 0 to Length(Conditions) - 1 do
  begin
    if not (Conditions[I] = '') then
      IBDataModule.IBQuery.SQL.add(Conditions[I]);
  end;
  IBDataModule.IBQuery.Open;
end;

end.
